<?php
/**
 * 轮播
 * User: 1874
 * Date: 2019/8/7
 * Time: 14:44
 */

namespace app\adminplatform\controller;

use app\adminplatform\model\AuthGroup;
use app\adminplatform\model\Log;
use app\adminplatform\model\Carousel as CarouselModel;
use think\facade\Config;
use think\facade\Request;
use think\facade\View;
use think\facade\Session;

class Carousel extends Initial {
    /*
     * 轮播列表
     */
    public function carouselList(){
        $map = [];
        //名称
        if(input('name')) $map[] = ['ca_title','like','%'.input('name').'%'];
        if(input('status') != null) $map[] = ['ca_status','=',input('status')];
        //查询
        $info = CarouselModel::carouselList($map);
        //管理员权限
        $rules = ['Carousel/carouselAdd', 'Carousel/carouselEdit', 'Carousel/carouselDel'];
        $check = AuthGroup::checkMethodArray(Session::get('userData'), $rules);

        View::assign('info',$info);
        View::assign('check',$check);
        return View::fetch('carouselList');
    }
    /*
     * 轮播新增
     */
    public function carouselAdd(){
        //判断请求类型
        if(Request::isPost()){
            $request = input();
            $map['ca_title']  = $request['title'];
            $map['ca_image']  = $request['image'];
            $map['ca_url']  = $request['url'];
            $map['ca_sort']  = $request['sort'];
            $map['ca_status']  = $request['status'];
            //检查信息是否完整
            checkField($map);
            //新增信息
            $info = CarouselModel::create($map);
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'新增轮播【'.$map['ca_title'].'】');

                $data = ['info'=>'新增成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，新增失败了','status'=>0];
            }
            return json($data);
        }
        return View::fetch('carouselAdd');
    }
    /*
     * 轮播编辑
     */
    public function carouselEdit(){
        $id = input('id');
        //判断请求类型
        if(Request::isPost()){
            $request = input();
            $map['ca_id']  = $id;
            $map['ca_title']  = $request['title'];
            $map['ca_image']  = $request['image'];
            $map['ca_url']  = $request['url'];
            $map['ca_sort']  = $request['sort'];
            $map['ca_status']  = $request['status'];
            //更换图片删除旧图片
            if($request['oldImage'] != '' && $request['oldImage'] != $request['image']){
                unlink(Config::get('app.upload_url').$request['oldImage']);
                //新图片赋值
                $map['ca_image']  = $request['image'];
            }
            //检查信息是否完整
            checkField($map);
            //编辑信息
            $info = CarouselModel::update($map, ["ca_id"=>$map["ca_id"]]);
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'编辑轮播【'.$map['ca_title'].'】');

                $data = ['info'=>'编辑成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，信息没有改变','status'=>0];
            }
            return json($data);
        }
        $info = CarouselModel::carouselInfo(["ca_id"=>$id]);

        View::assign('info',$info);
        return View::fetch('carouselEdit');
    }
    /*
     * 轮播删除
     */
    public function carouselDel(){
        //判断请求类型
        if(!Request::isPost()){
            $data = ['info'=>'请求类型不是这个哦，亲','status'=>0];
        }else{
            $id = input('id');
            //查询
            $carousel = CarouselModel::carouselInfo(["ca_id"=>$id]);
            //删除信息
            $info = $carousel->delete();
            if($info){
                //删除封面
                unlink(Config::get('app.upload_url').$carousel->ca_image);
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'删除轮播【'.$carousel->ca_title.'】');

                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，删除失败了','status'=>0];
            }
        }

        return json($data);
    }
    /*
     * 轮播图片上传
     */
    public function carouselUpload(){
        $website = website();
        //限制2M
        $data = uploadFile('file', Config::get('app.carousel_url'), $website["sy_photoMaxSize"]);

        return json($data);
    }
}